package m.jclecai.com.activity;

import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iapppay.sdk.main.IAppPay;
import com.squareup.okhttp.Request;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.base.recyclerview.BaseRecyclerViewAdapter;
import m.jclecai.com.base.recyclerview.BaseViewHolder;
import m.jclecai.com.bean.OrderDetailBean;
import m.jclecai.com.bean.PayChannelBean;
import m.jclecai.com.bean.RecordsBean;
import m.jclecai.com.bean.ResponsePayWayList;
import m.jclecai.com.bean.UserBean;
import m.jclecai.com.bean.WechatConfig;
import m.jclecai.com.bean.WechatInfo;
import m.jclecai.com.callback.ClickCallBack;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.dialog.WaitPayOrderDialog;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.pay.AiBeiPayImpl;
import m.jclecai.com.pay.BasePayImpl;
import m.jclecai.com.pay.PayWithQrCodePayImpl;
import m.jclecai.com.pay.RedirectPayImpl;
import m.jclecai.com.pay.RequestPayImpl;
import m.jclecai.com.pay.WeiFuTongPayImpl;
import m.jclecai.com.service.DownloadService;
import m.jclecai.com.utils.ChannelUtil;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.ScreenUtil;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import rx.Subscription;
import rx.functions.Action1;

public class WechatDetailActivity extends BaseActivity {



    @BindView(R.id.rv)
    RecyclerView mRvPayChannel;

    private String Str = "";


    private PayChannelAdapter mPayChannelAdapter;


    @Override
    protected int getContentView() {
        return R.layout.wechat_friend_info_item;
    }


    @Override
    public void initData() {

        Str = getResources().getString(R.string.tv_title);
        showDialog();
        senReq();
    }




    WechatInfo payWayList = null;
    List<WechatConfig> wc = new ArrayList<WechatConfig>();
    /**
     * 初始化支付的渠道；
     */
    private void senReq() {
        //这里我们请求网络来决定有几种充值方式


        HttpManager.getAsyn(Constant_Url.APP_CONFIG_URL, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay("请检查网络！！！");
                dissDialog();
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                if (!TextUtils.isEmpty(response)) {
                    payWayList = FastOk.get(response, WechatInfo.class);
                    wc = payWayList.getPayment();
//                    payWays.addAll();
                } else {
                    DisPlay("服务器异常，稍后重试！！");
                }
                //刷新数据；
                if (mPayChannelAdapter != null){
                    if (wc!=null&&wc.size()>0&&!wc.equals("[]")){
                        mPayChannelAdapter.refresh(wc);
                    }

                }
            }
        });
    }








    @Override
    public void initView() {
        setTitle("提示");
        leftOptionText("");
        rightOptionText("打开微信");
        setClickCallBack(new ClickCallBack() {
            @Override
            public void OnClick() {
                getWechatApi();
                finish();
            }
        });
        mRvPayChannel.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mPayChannelAdapter = new PayChannelAdapter(null);
        mRvPayChannel.setAdapter(mPayChannelAdapter);

    }






    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

//        if (data == null) {
//            DisPlay("未知错误");
//            return;
//        }
//        dissDialog();
//        String respCode = data.getExtras().getString("resultCode");
//        if (!TextUtils.isEmpty(respCode) && respCode.equalsIgnoreCase("success")) {
//            chargeStatus = RESULT_OK;
//            //标示支付成功
//            DisPlay("支付成功");
//        } else { //其他状态NOPAY状态：取消支付，未支付等状态
//            chargeStatus = RESULT_CANCELED;
//            DisPlay("未支付");
//        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    protected void close() {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    //==============充值的adapter====================

    public class PayChannelAdapter extends BaseRecyclerViewAdapter<WechatConfig> {

        public int mCheckedPosition;

        public PayChannelAdapter(List<WechatConfig> DList) {
            super(DList);
        }

        @Override
        public int getItemView() {
            return R.layout.wechat_num_item;
        }

        @Override
        protected void bindViewHolder(BaseViewHolder holder, final WechatConfig bean, final int position) {

            holder.getTextView(R.id.tv_one).setText(
                    Html.fromHtml(String.format(Str,bean.getTitle(),""))
            );
            if (bean.getTitle().contains("咨询")||bean.getTitle().contains("资讯")){
                holder.getTextView(R.id.tv_two).setText("微信号  "+bean.getValue()+"  (咨询问题专用通道)");
            }else{
                holder.getTextView(R.id.tv_two).setText("微信号  "+bean.getValue());
            }



//            holder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    getDataList().get(mCheckedPosition).setChecked(false);
//                    mCheckedPosition = position;
//                    getDataList().get(position).setChecked(true);
//                    notifyDataSetChanged();
//                }
//            });

        }

        public int getCheckedPosition() {
            return mCheckedPosition;
        }

        public WechatConfig getCheckedBean() {
            WechatConfig payChannelBean = null;
            if (null!=mDList&&mDList.size()>0){
                payChannelBean = mDList.get(mCheckedPosition);
            }
            return payChannelBean;
        }
    }



    /**
     * 跳转到微信
     */
    private void getWechatApi(){
        try {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            ComponentName cmp = new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(cmp);
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            // TODO: handle exception
            ToastUtil.showToast(WechatDetailActivity.this,"检查到您手机没有安装微信，请安装后使用该功能");
        }
    }


}
